//
//  ZH_GoodsInfoView.m
//  WisdomStar
//
//  Created by zhangliwen on 2018/11/14.
//  Copyright © 2018年 zhangliwen. All rights reserved.
//

#import "ZH_GoodsInfoView.h"
#import "ZH_GoodsinfoTableViewCell.h"
#import "ZH_GoodsNumTableViewCell.h"
@interface ZH_GoodsInfoView()<UITableViewDelegate, UITableViewDataSource>
{
    NSArray *_privateTitleArray;
}
///tableview
@property (nonatomic, strong) UITableView *conditionTableView;
@property (nonatomic,assign)NSInteger index;
@property (nonatomic,strong)NSMutableArray *array;
@property (nonatomic,strong)NSString *string1;
@property (nonatomic,strong)NSString *string2;
@property (nonatomic,strong)NSString *string3;
@property (nonatomic,strong)NSString *titleString;
@property (nonatomic,strong)NSString *numString;

@end
@implementation ZH_GoodsInfoView


- (instancetype)init{
    if (self = [super init]) {
        self.backgroundColor = [UIColor colorWithWhite:0.7 alpha:0.3];
        [self initSubviews];
    }
    return self;
}
- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        
        self.backgroundColor = [UIColor colorWithWhite:0.7 alpha:0.3];
        //        self.alpha = 0.3;
        //        self.clipsToBounds = NO;
        [self initSubviews];
    }
    return self;
}
#pragma mark public
- (void)showConditionChoseViewOnFather:(UIView *)fatherView{
    [fatherView addSubview:self];
    self.frame = CGRectMake(0, 0, SCREEN_WIDTH, fatherView.frame.size.height);
    self.conditionTableView.frame = CGRectMake(0, fatherView.frame.size.height - FITSCALE(450), SCREEN_WIDTH, FITSCALE(450));
    //[self.conditionTableView reloadData];
}
- (void)dissmissConditionChoseViewOnFather:(UIView *)fatherView{
    [self removeFromSuperview];
}
#pragma mark subviews
- (void)initSubviews{
    [self addSubview:self.conditionTableView];
    self.conditionTableView.frame = CGRectMake(0, 0, SCREEN_WIDTH, 0);
}
#pragma mark touch
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    //    [UIView animateWithDuration:0.1 animations:^{
    //        self.conditionTableView.frame = CGRectMake(0, 0, SCREEN_WIDTH, 0);
    //    } completion:^(BOOL finished) {
    //        //[self.conditionTableView removeFromSuperview];
    //
    //    }];
    [self removeFromSuperview];
}
- (void)update:(NSMutableArray *)data{
    if (data.count > 0) {
        [self.array removeAllObjects];
        [self.array addObjectsFromArray:data];
        [self.conditionTableView reloadData];
    }
}
#pragma mark tableviewdelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return self.array.count;
    }
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return CGFLOAT_MIN;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return CGFLOAT_MIN;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        ZH_GoodsinfoTableViewCell *cell = [ZH_GoodsinfoTableViewCell cellWithTableView:tableView];
        [cell updateUiForData:self.array[indexPath.row]];
        
        
        cell.onClickBlock = ^(NSString *title) {
           
            if (indexPath.row == 0) {
                self.string1 =[NSString stringWithFormat:@"%@-%@",self.array[indexPath.row][@"id"],title];
            }
            else if (indexPath.row == 1){
                self.string2 =[NSString stringWithFormat:@"%@-%@",self.array[indexPath.row][@"id"],title];
            }
            else if (indexPath.row == 2){
                self.string3 =[NSString stringWithFormat:@"%@-%@",self.array[indexPath.row][@"id"],title];
            }
            NSString *string = [NSString stringWithFormat:@"%@-%@-%@",self.string1,self.string2,self.string3];
            
            [self changeTitle:string andIndex:indexPath.row];
        };
        return cell;
    }
    else if (indexPath.section == 1){
        ZH_GoodsNumTableViewCell *cell = [ZH_GoodsNumTableViewCell cellWithTableView:tableView];
        cell.onButtonClick = ^(NSString *num) {
            self.numString = num;
            
        };
        cell.onButtonClicks = ^{
            if (self.onButtonClick) {
                self.onButtonClick(self.numString, self.titleString);
            }
            [self removeFromSuperview];
        };
        return cell;
    }
    return nil;
}
- (void)changeTitle:(NSString *)title andIndex:(NSInteger)index{
    self.titleString = title;
    NSLog(@"%@",title);
    if (self.chosedBlock) {
        self.chosedBlock(title);
    }
}
#pragma mark lazy
- (UITableView *)conditionTableView{
    if (!_conditionTableView) {
        _conditionTableView = [UITableView createTableViewWithDelegateTarget:self andTableViewStyle:UITableViewStylePlain];
        [_conditionTableView setCellAutoAdaptationForEstimatedRowHeight:FITSCALE(100)];
        self.backgroundColor = kCommonWhiteBg;
        //[_conditionTableView registerClass:[RH_ConditionTableViewCell class] forCellReuseIdentifier:NSStringFromClass([RH_ConditionTableViewCell class])];
    }
    return _conditionTableView;
}
- (BOOL)isShow{
    return self.superview;
}
- (NSMutableArray *)array{
    if (!_array) {
        _array = [[NSMutableArray alloc] initWithCapacity:0];
    }
    return _array;
}
@end
